package org.boot.ssm.wxshop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.GoodsTypeBig;
import org.boot.ssm.wxshop.domain.GoodsTypeSmall;
import org.boot.ssm.wxshop.exception.BusinessException;
import org.boot.ssm.wxshop.mapper.GoodsTypeBigMapper;
import org.boot.ssm.wxshop.mapper.GoodsTypeSmallMapper;
import org.boot.ssm.wxshop.service.GoodsTypeBigService;
import org.boot.ssm.wxshop.service.GoodsTypeSmallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: wxshop
 * @description: GoodsTypeBigService的实现类
 * @author: Parasomnia
 * @create: 2018-03-13 22:02
 **/
@Service
public class GoodsTypeBigServiceImpl extends BaseServiceImpl<GoodsTypeBig> implements GoodsTypeBigService{
    @Autowired
    private GoodsTypeBigMapper goodsTypeBigMapper;

    @Autowired
    private GoodsTypeSmallMapper goodsTypeSmallMapper;

    @Override
    public PageInfo<GoodsTypeBig> queryPageListByName(Integer pageNo, Integer pageSize, GoodsTypeBig goodsTypeBig) {
        PageHelper.startPage(pageNo, pageSize);
        List<GoodsTypeBig> list=goodsTypeBigMapper.selectByName(goodsTypeBig);
        // 用PageInfo对结果进行包装
        PageInfo<GoodsTypeBig> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public void addOrEditType(GoodsTypeBig goodsTypeBig) {
        if(goodsTypeBig.getId()==null){//添加
            saveNoNull(goodsTypeBig);
        }else{//修改
            updateNoNull(goodsTypeBig);
        }
    }

    @Override
    public void deleteWithCheck(GoodsTypeBig goodsTypeBig) {
        GoodsTypeSmall temp = new GoodsTypeSmall();
        temp.setGoodsTypeId(goodsTypeBig.getId());
        temp.setIsDelete(1);
        int size = goodsTypeSmallMapper.select(temp).size();
        if(size>0)
            throw new BusinessException("当前分类下存在子分类，无法删除");
        updateDelete(goodsTypeBig);
    }
}
